﻿
$(function () {
    CommonTree("dept", "dept","showDept");
    loadData();
    autoHeight("divLeft");
})
/***
 * 人员同步
 * **/
function SyncUser() {
    SyncData("user");
}
function openWin() {
    var dept = $("input[ name='dept']").val();
    InfoOpenPage("../System/UpdateUser?dataId=0&dept=" + dept, "用户添加", "65%", "80%",true);
}

function deptShow() {
    alert("暂不开放");
}
var currPage = 1;//首先默认值为1，防止出错
//赋值用户列表
function loadData() {
    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function () {
        var laydate = layui.laydate //日期
            , laypage = layui.laypage //分页
            , layer = layui.layer //弹层
            , table = layui.table //表格
        var condition = JSON.stringify(FormToJson("qForm"));
        var dUrl = '../System/getDataList?dataType=user&condition=' + condition;

        //给table赋值
        table.render({
            elem: '#userList'
            , id: "tData"

            //, width: 1030
            , url: dUrl //数据接口


            , title: '系统用户表'
            , page: true //开启分页
            , even: true
            , limits: [15, 20, 30]
            , limit: 15 //每页默认显示的数量
            , cols: [[ //表头
                { field: 'userCode', title: '登录账号', width: 100, sort: true }
                , { field: 'userName', title: '用户名称', width: 100 }
                , { field: 'deptName', title: '用户部门', width: 110 }
                , { field: 'userPhone', title: '联系方式', width: 110 }
                , { field: 'createTime', title: '创建时间', width: 170, sort: true, }
                , { field: 'statusName', title: '是否在职', width: 90 }
                , { field: 'isSyncName', title: '同步数据', width: 90 }
                , { fixed: 'right', title: '操作', width: 100, align: 'center', toolbar: '#barDemo' }
            ]], done: function (res, curr, count) {
                currPage = curr;
                $.each(res['data'], function (i, obj) {
                    var status = obj["status"];
                    if (status == "N") {
                        TableRowColor('layui-table-main', i, '#bababa');
                    }
                });
            }
        });

        //监听行工具事件
        table.on('tool(ulist)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == 'reSetPwd') {
                if (data.isAcc == "1") {
                    layer.msg("同步数据禁止重置密码");
                    return;
                }
                layer.confirm('确认重置密码？', {
                    btn: ['是', '否'] //可以无限个按钮
                    , btn2: function (index, layero) {
                        //否
                    }
                }, function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "../System/ReSetPwd",
                        data: { userCode: data.userCode },
                        type: "post",
                        success: function (data) {
                            if (data.result) {
                                layer.msg("密码重置成功");
                            } else {
                                layer.msg(data.message);
                            }
                        }
                    });
                });
            } else if (layEvent == 'userEdit') {
                if (data.isSync == "Y") {
                    layer.msg("同步数据禁止修改");
                } else {
                    var str = data.userName + "-修改";
                    NewOpenPage("../System/UpdateUser?dataId=" + data.id, str, "80%", "90%", "tData", currPage, dUrl);
                }

            } else if (layEvent == 'userDel') {
                layer.confirm('确认删除吗？', {
                    btn: ['是', '否'] //可以无限个按钮
                    , btn2: function (index, layero) {
                        //否
                    }
                }, function (index) {
                    $.ajax({
                        url: "../System/DelUserInfo",
                        type: "post",
                        data: { userCode: data.userCode },
                        success: function (rsl) {
                            if (rsl.result) {
                                layer.msg("删除成功");
                            } else {
                                layer.msg("删除失败");
                            }
                            loadData();
                        }
                    });
                    layer.close(index);
                });
            } else if (layEvent == 'setPower') {
                var str = data.userName + "(" + data.userCode + ")-权限配置";
                NewOpenPage("../System/UserPower?userCode=" + data.userCode + "&roleCode=" + data.powerRole, str, "80%", "90%", "tData", currPage, dUrl)
            } 
        });
    });
}